#!/bin/bash
#

# 判断是否存在密钥
if [ -f /root/.ssh/id_rsa ]; then
   rm -rf /root/.ssh/id_rsa*
fi

# 生成密钥对
ssh-keygen -t rsa -f /root/.ssh/id_rsa -P "" &> /dev/null

#向目标主机发送公钥
if [ -f /root/.ssh/known_hosts ]; then
   rm -rf /root/.ssh/known_hosts
fi

for i in 138 141; do
/usr/bin/expect << eof
set timeout 10
spawn ssh-copy-id root@192.168.159.$i
expect "(yes/no)?"
send "yes\n"
expect "password:"
send "123456\n"
expect eof
eof
done

echo "-------------------------------------------"
# 验证
for i in 138 141; do
   ssh root@192.168.159.$i hostname;date
done
echo "-------------------------------------------"

